package cn.jbolt.admin.appdevcenter;

import cn.jbolt.core.api.JBoltApiBaseController;
import cn.jbolt.core.api.httpmethod.JBoltHttpGet;
import cn.jbolt.core.api.httpmethod.JBoltHttpPost;
import cn.jbolt.core.base.JBoltMsg;
import cn.jbolt.core.bean.JBoltAppLinkTarget;
import cn.jbolt.core.common.enums.JBoltApplicationType;
import cn.jbolt.core.enumutil.JBoltEnum;
import cn.jbolt.core.model.Application;
import cn.jbolt.core.model.base.JBoltBaseModel;
import cn.jbolt.core.permission.CheckPermission;
import cn.jbolt.core.permission.OnlySaasPlatform;
import cn.jbolt.core.permission.UnCheck;
import cn.jbolt.core.permission.UnCheckIfSystemAdmin;
import cn.jbolt.core.service.JBoltApplicationService;
import cn.jbolt.core.service.base.JBoltBaseService;
import cn.jbolt.extend.permission.PermissionKey;
import com.jfinal.aop.Before;
import com.jfinal.aop.Inject;
import com.jfinal.core.Path;
import com.jfinal.core.paragetter.Para;
import com.jfinal.plugin.activerecord.tx.Tx;

@UnCheckIfSystemAdmin
@OnlySaasPlatform
@CheckPermission(PermissionKey.APPLICATION)
@Path("/api/admin/app")
public class ApplicationAdminController extends JBoltApiBaseController {
    @Inject
    private JBoltApplicationService service;

    @Override
    protected JBoltBaseService<? extends JBoltBaseModel<?>> getModelService() {
        return service;
    }

    /**
     * 单个详情
     */
    @JBoltHttpGet
    public void index() {
        renderJBoltApiRet(findModelById());
    }

    /**
     * 分页条件查询数据源
     * @param page
     * @param pageSize
     * @param keywords
     * @param type
     * @param enable
     */
    @JBoltHttpGet
    public void datas(@Para(value = "page",defaultValue = "1")Integer page,
                      @Para(value = "pageSize",defaultValue = "15")Integer pageSize,
                      @Para("type")Integer type,
                      @Para("enable")Boolean enable,
                      @Para("keywords")String keywords) {
        renderJBoltApiSuccessWithData(service.paginateAdminList(page,pageSize,keywords,type,enable));
    }


    @JBoltHttpGet
    public void linkTarget() {
        Application application=service.findById(getLong(0));
        if(application==null) {
            renderJBoltApiFail(JBoltMsg.DATA_NOT_EXIST);
            return;
        }
        set("application", application);
        Object linkTarget = service.getLinkTarget(application.getType(), application.getLinkTargetId());
        renderJBoltApiSuccessWithData(linkTarget);
    }

    /**
     * 解除关联对象
     */
    @JBoltHttpGet
    public void removeLinkTarget() {
        renderJson(service.removeLinkTarget(getLong(0)));
    }

    /**
     * select数据源options enable=true的
     */
    @UnCheck
    @JBoltHttpGet
    public void options() {
        renderJBoltApiSuccessWithData(service.getOptionListEnable());
    }

    /**
     * 保存
     */
    @Before(Tx.class)
    @JBoltHttpPost
    public void save(@Para("") Application application) {
        renderJBoltApiRet(service.save(application));
    }

    /**
     * 更新
     */
    @Before(Tx.class)
    @JBoltHttpPost
    public void update(@Para("")Application application) {
        renderJBoltApiRet(service.update(application));
    }

    /**
     * 删除
     */
    @Before(Tx.class)
    @JBoltHttpGet
    public void delete() {
        renderJBoltApiRet(service.deleteById(getId(),true));
    }

    /**
     * 切换启用状态
     */
    @Before(Tx.class)
    @JBoltHttpGet
    public void toggleEnable() {
        renderJBoltApiRet(service.toggleEnable(getId()));
    }

    /**
     * 切换是否需要接口needchecksign
     */
    @Before(Tx.class)
    @JBoltHttpGet
    public void toggleNeedCheckSign() {
        renderJBoltApiRet(service.toggleBoolean(getId(),"need_check_sign"));
    }
    /**
     * 变更AppSecret
     */
    @Before(Tx.class)
    @JBoltHttpGet
    public void changeAppSecret() {
        renderJBoltApiRet(service.changeAppSecret(getId()));
    }

    /**
     * Api应用中心应用类型列表数据源
     */
    @JBoltHttpGet
    public void types() {
        renderJBoltApiSuccessWithData(JBoltEnum.getEnumOptionList(JBoltApplicationType.class));
    }

    /**
     * 应用类型数据源 form中使用
     * 排除了innerplatform
     */
    @JBoltHttpGet
    public void typesInForm(){
        renderJBoltApiSuccessWithData(JBoltEnum.getEnumOptionListWithout(JBoltApplicationType.class,JBoltApplicationType.INNER_PLATFORM.getValue()));
    }

}
